package com.core.license;

import com.core.util.DspUtil;
import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.ArrayUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 得到系统Mac地址, 操作系统 , IP地址, 主机名
 * Created by IntelliJ IDEA
 * User: laker
 * Date: 11-12-3
 * Time: 下午5:16
 */
public class SystemInfo {

    /**
     * 是否为Windows操作系统平台
     *
     * @return
     */
    public static final boolean isWindows() {
        return getOsPlatform().indexOf("windows") > -1;
    }

    /**
     * 查看该主机的操作系统类型
     *
     * @return
     */
    public static final String getOsPlatform() {
        return System.getProperty("os.name");
    }

    /**
     * 得到系统IP地址
     *
     * @return
     */
    public static final String getIPAddress() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 得到Mac地址,该方法适合Windows,Linux,Unix三大平台
     *
     * @return 该主机的所有Mac地址, 包含虚拟地址
     */
    public static final String getMacAddress() {
        String os = System.getProperty("os.name").toLowerCase();
        String output = null;
        String cmd = "ipconfig /all";
        if (os.indexOf("windows") == -1) {
            cmd = "ifconfig";
        }
        byte[] r = null;
        StringBuffer sb = new StringBuffer();
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            InputStream is = proc.getInputStream();
            byte[] __b = read(is, DspUtil.getFileEncode());
            output = new String(__b);
        } catch (Exception e) {
            cmd = "ipconfig /all";
            if (os.indexOf("windows") < 0)
                cmd = "/sbin/ifconfig";
            Process proc = null;
            try {
                proc = Runtime.getRuntime().exec(cmd);
                InputStream is = proc.getInputStream();
                byte[] __b = read(is, DspUtil.getFileEncode());
                output = new String(__b);
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        //System.out.println(output);
        Pattern p = Pattern.compile("([0-9a-zA-z]{2}[\\:\\-]){5}[0-9a-zA-z]{2}", 32);
        Matcher m = p.matcher(output);
        int lastIndex = 0;
        while (m.find(lastIndex)) {
            if (lastIndex != 0) {
                sb.append(",");
            }
            sb.append(m.group(0));
            lastIndex = m.end();
        }

        return sb.toString();
    }

    public static byte[] read(InputStream is, String encoding) {
        try {
            byte[] bs = readBytes(is);
            return bs;
            /**if (("utf-8".equalsIgnoreCase(encoding)) &&
                    (DspUtil.hexEncode(ArrayUtils.subarray(bs, 0, 3)).equals("efbbbf"))) {
                bs = ArrayUtils.subarray(bs, 3, bs.length);
            }
            return new String(bs);**/
        } catch (Exception e) {
            DspUtil.getLogger().error(e.getLocalizedMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    private static byte[] readBytes(InputStream is) {
        byte[] r = null;
        try {
            r = new byte[is.available()];
            is.read(r);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return r;
    }

    public static void main(String[] args) {
        try {
            System.out.println(" Operating System :" + System.getProperty("os.name"));
            System.out.println(" Host Name:"         + InetAddress.getLocalHost().getHostName());
            System.out.println(" IP/Localhost:"      + InetAddress.getLocalHost().getHostAddress());
            System.out.println(" Mac Address :"      + SystemInfo.getMacAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
